package no.uib.kwikktaim.gui.actionlisteners;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import no.uib.kwikktaim.core.FrameHandler;
import no.uib.kwikktaim.gui.MainWindow;

public class OpenFile extends JDialog implements ActionListener {

	private MainWindow mw;
	JPanel container1, container2,container3, container4,container5;
	JPanel wholeDialog;
	JButton buttonOpenFile,buttonOK,buttonCancel;
	JTextField textField;
	File f;

	public OpenFile(MainWindow caller) {
		mw = caller;

		this.setLayout(new BorderLayout());
		this.setModal(true);


		container1 = new JPanel(new BorderLayout());
		container2 = new JPanel(new BorderLayout());
		container3 = new JPanel(new BorderLayout());
		container4 = new JPanel(new BorderLayout());
		container5 = new JPanel(new FlowLayout());
		wholeDialog = new JPanel(new BorderLayout());


		buttonOpenFile = new JButton("Browse");
		buttonOK = new JButton("OK");
		buttonCancel = new JButton("Cancel");
		textField = new JTextField("http://");
		JLabel visitURL = new JLabel("URL to open:");
		JLabel openIcon = new JLabel();
		openIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/kwikktaim/img/document-open.png")));


		buttonOpenFile.addActionListener(this);
		buttonOK.addActionListener(this);
		buttonCancel.addActionListener(this);

		container1.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 10));
		container2.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 5));
		container3.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));
		container4.setBorder(BorderFactory.createEmptyBorder(15, 10, 10, 10));

		container3.add(textField,BorderLayout.CENTER);

		container1.add(visitURL, BorderLayout.NORTH);
		container1.add(container3,BorderLayout.WEST);
		container1.add(buttonOpenFile , BorderLayout.EAST);
		container5.add(buttonOK);
		container5.add(buttonCancel);
		container2.add(container5,BorderLayout.EAST);

		container4.add(openIcon,BorderLayout.CENTER);


		this.add(container1, BorderLayout.CENTER);
		this.add(container2, BorderLayout.SOUTH);
		this.add(container4, BorderLayout.WEST);


		textField.setPreferredSize(new Dimension(250,20));

		this.setResizable(false);

		this.pack();

		FrameHandler.centerScreen(this);
	}

	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource().equals(buttonOpenFile)) {
			JFileChooser fc = new JFileChooser(mw.saved.getCurrentFolder());
			fc.showOpenDialog(mw);
			f = fc.getSelectedFile();
			if(f!=null)  textField.setText(f.getAbsolutePath());
			if(f == null) return;
		}

		if(arg0.getSource().equals(buttonOK)) {

			textField.getText();
			this.setVisible(false);
			File f = new File(textField.getText());
			try {
				mw.saved.setCurrentFolder(textField.getText());
				if(f.exists()) {
					mw.addMediaToPlayList(f.toURI().toURL());
				}
				else {
					mw.addMediaToPlayList(URI.create(textField.getText()).toURL());
				}

				mw.currentlyPlaying++;
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		if(arg0.getSource().equals(buttonCancel)) {
			this.dispose();
			this.setVisible(false);
		}
		if(arg0.getSource().equals(mw.buttonAdd)) {
			this.setVisible(true);
		}
	}
}
